from django.template import Library

from projects.models import Deliverable, Objective, Task, WorkPackage
from projects import utils

register = Library()

@register.inclusion_tag("workpackage_table.html")
def list_work_packages(work_packages):
  return {'work_package_list': work_packages.order_by('end_date', 'start_date')}

@register.inclusion_tag("aim_table.html")
def list_work_package_aims(work_package):
  set = work_package.aim_set.all()
  return {'aim_list': set}

@register.inclusion_tag("rating.html")
def work_package_status(work_package):
  assert isinstance(work_package, WorkPackage)
  incomplete_tasks = Task.objects.filter(deliverable__objective__aim__work_package__id = work_package.id, completed_date = None)
  incomplete_deliverables = Deliverable.objects.filter(objective__aim__work_package__id = work_package.id, completed_date = None)
  objectives = Objective.objects.filter(aim__work_package__id = work_package.id)
  return utils.get_ratings("work_package", incomplete_tasks, incomplete_deliverables, objectives)
